package com.hl.transportationhelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.SQLException;

public class ActivityGioTau extends Activity  {
	
	public static String EXTRA_MESSAGE = "test";
	private Button btnXem;
	private GridView gridGioTau;
	private Spinner spinnerGaDi, spinnerTuyenTau, spinnerGaDen, spinnerMaTau;
	private ArrayList<String> listGaDi = null;
	private ArrayList<String> listGaDen = null;
	private ArrayList<String> listMaTau = null;
	private List<String> listGioTau = null;
	final DatabaseHandler db = new DatabaseHandler (this);
	String arrTableName[] = {"gt_Hanoi_Saigon",
    		"gt_Saigon_Hanoi",
    		"gt_Hanoi_Laocai",
    		"gt_Laocai_Hanoi",
    		"gt_Hanoi_Haiphong",
    		"gt_Haiphong_Hanoi",
    		"gt_Hanoi_Dongdang",
    		"gt_Dongdang_Hanoi",
    		"gt_Hanoi_Quantrieu",
    		"gt_Quantrieu_Hanoi" };
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gio_tau);        
       
        
        addItemOnSpnTuyenTau();
        //createDBGioTau();
        
        
        
        btnXem = (Button) findViewById(R.id.btnXacNhan);
        btnXem.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				listGioTau = new ArrayList<String>();
				if(spinnerTuyenTau.getSelectedItemPosition() == 0){
					
					Toast.makeText(getBaseContext(), "Bạn phải chọn một tuyến", Toast.LENGTH_LONG).show();
				}else{
					
					
					//grid
			        //String[] array = new String[] {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
			        //List<String> list = new ArrayList<String>(Arrays.asList(array));
					db.getGioTau(arrTableName[spinnerTuyenTau.getSelectedItemPosition()-1],
						 spinnerMaTau.getSelectedItem().toString(), 
							 spinnerGaDen.getSelectedItemPosition() +2,
							 spinnerGaDi.getSelectedItemPosition(),
							 listGioTau);
					
					gridGioTau = (GridView) findViewById(R.id.grdGioTau);
			        ArrayAdapter<String> gridAdapter = new ArrayAdapter<String>(ActivityGioTau.this, R.layout.list_item, listGioTau);
			        gridGioTau.setAdapter(gridAdapter);
			        
				}
			}
		});
        
        Button btnXemGioTau = (Button) findViewById(R.id.btnCheck);
        btnXemGioTau.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				Intent nextXemGioTau = new Intent(v.getContext(),ActivityXemGioTau.class);
				String test = null; //= db.getString();
				test = db.getString();
				nextXemGioTau.putExtra(EXTRA_MESSAGE, test);
				
				startActivity(nextXemGioTau);
				
			}
		});      
        
        
    }
    
    public void createDBGioTau() {
		
		AssetManager am = this.getAssets();
		
		try {
			
			for (int i = 0; i < arrTableName.length; i++) {
				
				String strTBName =  arrTableName[i] +".html";
				db.createTableGioTau(arrTableName[i],strTBName, am);
				
			}		
		
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
	}

    public void addItemOnSpnTuyenTau() {
    	
    	
    	spinnerTuyenTau = (Spinner) findViewById(R.id.spinnerTuyenTau);
    	
    	ArrayAdapter<CharSequence> spinnerAdapterTuyenTau = ArrayAdapter.createFromResource(this,
    			R.array.routePrompt, android.R.layout.simple_spinner_dropdown_item);
    	
    	spinnerAdapterTuyenTau.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinnerTuyenTau.setAdapter(spinnerAdapterTuyenTau); 
    	spinnerTuyenTau.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				
				Boolean isValidPosGaDi = false;
				if(position == 0){
					
					Toast.makeText(parent.getContext(), "Bạn phải chọn một tuyến", Toast.LENGTH_LONG).show();
					addItemOnSpnMaTau(false);
					
				}else{
					
					isValidPosGaDi = true;
					addItemOnSpnMaTau(true);
					
					}
					
				if(isValidPosGaDi)				
					addItemOnSpnGaDi(isValidPosGaDi);
				else
					addItemOnSpnGaDi(isValidPosGaDi);
				
				
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
    		
		});
    	
		
	}
    
    public void addItemOnSpnGaDi(boolean check) {
    	
    	listGaDi = new ArrayList<String>();
    	spinnerGaDi = (Spinner) findViewById(R.id.spinnerGaDi);    	    	
    	//Boolean isValidPosGaDen = true;
    	
    	if(check == true){
    	spinnerGaDi.setClickable(true);
    	db.getGaDi(arrTableName[spinnerTuyenTau.getSelectedItemPosition()-1], listGaDi);
    	
    	ArrayAdapter<String> spinnerAdapterGaDi = new ArrayAdapter<String>(this,
    			android.R.layout.simple_spinner_dropdown_item, listGaDi);
    	
    	spinnerAdapterGaDi.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinnerGaDi.setAdapter(spinnerAdapterGaDi); 
    	spinnerGaDi.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				addItemOnSpnGaDen(true);
				
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
    		
		});
    	
    	}else{
    		spinnerGaDi.setClickable(false);
    		listGaDi.clear();
    		spinnerGaDi.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,listGaDi));
    		
    		addItemOnSpnGaDen(false);
    		
    		
    	}
		
	}
    
   public void addItemOnSpnGaDen(Boolean check){
    	    	
    	listGaDen = new ArrayList<String>();
    	spinnerGaDen = (Spinner) findViewById(R.id.spinnerGaDen);
    	
    	if(check == true){
    		spinnerGaDen.setClickable(true);
    		listGaDen = new ArrayList<String>(listGaDi.subList(spinnerGaDi.getSelectedItemPosition()+1, listGaDi.size()));
	    	ArrayAdapter<String> spinnerAdapterGaDen = new ArrayAdapter<String>(this,
	    			android.R.layout.simple_spinner_dropdown_item,  listGaDen);
	    	
	    	spinnerAdapterGaDen.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	spinnerGaDen.setAdapter(spinnerAdapterGaDen);
	    	spinnerGaDen.setOnItemSelectedListener(new OnItemSelectedListener() {
	
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					
				}
	
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
	    	
	    }else{
	    	
	    	spinnerGaDen.setClickable(false);
    		listGaDen.clear();
    		spinnerGaDen.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, listGaDen));
	    }
    	
    }
    
   public void addItemOnSpnMaTau(Boolean check){
	   
	   listMaTau = new ArrayList<String>();
	   spinnerMaTau = (Spinner) findViewById(R.id.spinnerMaTau);
	   
	   if(check == true){
		   
		  spinnerMaTau.setClickable(true);
		  db.getMaTau(arrTableName[spinnerTuyenTau.getSelectedItemPosition()-1], listMaTau);
		  ArrayAdapter<String> spinnerAdapterMaTau = new ArrayAdapter<String>(this,
	  			android.R.layout.simple_spinner_dropdown_item,  listMaTau.subList(1, listMaTau.size()));
		  
		  spinnerAdapterMaTau.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		  spinnerMaTau.setAdapter(spinnerAdapterMaTau);
		  spinnerMaTau.setOnItemSelectedListener(new OnItemSelectedListener() {
	
			public void onItemSelected(AdapterView<?> parent, View view,
					int position,long id) {
				
				
			}
	
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
	
			
		});
	  }else{
		  
		  spinnerMaTau.setClickable(false);
		  listMaTau.clear();
		  spinnerMaTau.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, listMaTau));
	  }
	   
   }


    
   

}
